package com.crm.controller.client;

import com.crm.excel.export.ExportExcel;
import com.crm.exception.ServiceException;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.Contact;
import com.crm.service.client.ClientInfoService;
import com.crm.vo.ClientGroupCountVo;
import com.crm.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.crm.exception.ServiceException;
import com.crm.pojo.ClientInfo;
import com.crm.pojo.Contact;
import com.crm.pojo.Userinfo;
import com.crm.service.client.ClientInfoService;
import com.crm.service.client.impl.ClientInfoServiceImpl;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ClientInfoController {

	@Autowired
	public ClientInfoService clientInfoService;

	/**
	 * 转到客户管理页面
	 * 
	 * @return
	 */
	@GetMapping("/to-client")
	public String toClient() {
		return "client/client";
	}

	/**
	 * 获取客户列表
	 * 
	 * @return
	 */
	@GetMapping("/to-client-Datagrid")
	public String toClientDatagrid() {
		return "client/client-Datagrid";
	}

	/**
	 * 显示新建客户表单
	 * 
	 * @return
	 */
	@GetMapping("/to-add-client-form")
	public String toAddClinet() {
		return "client/add-client-form";
	}

	/**
	 * 显示新建客户表单
	 * 
	 * @return
	 */
	@GetMapping("/to-edit-client-form")
	public String toEditClinet() {
		return "client/edit-client-form";
	}

	/**
	 * 显示新建客户表单
	 * 
	 * @return
	 */
	@GetMapping("/to-view-client")
	public String toViewClient() {
		return "client/view-client";
	}

	/**
	 * 获取所有得用户
	 */
	@PostMapping("/client-get-all-user")
	@ResponseBody
	public List<Userinfo> queryAllUser() {
		return clientInfoService.queryAllUser();
	}
	
	@PostMapping("/find-all-client")
	@ResponseBody
	public List<ClientInfo> findAllNotDel() {
		return clientInfoService.findAllNotDel();
	}

	/**
	 * 查找用户，可以是clientInfo的任意属性
	 * 
	 * @param clientInfo
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@PostMapping("/find-all-clint-info")
	@ResponseBody
	public Map queryByClientInfo(ClientInfo clientInfo, int page, int rows) {
		Long total = clientInfoService.queryNumByClientInfo(clientInfo, null, null, null, null);
		List<ClientInfo> list = clientInfoService.queryByClientInfo(clientInfo, page, rows, null, null, null, null);
		@SuppressWarnings("rawtypes")
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 查找用户，可以是clientInfo的任意属性
	 * 
	 * @param clientInfo
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@PostMapping("/advance-search-client-info")
	@ResponseBody
	public Map queryByClientInfo(ClientInfo clientInfo, int page, int rows, Date createStartTime, Date createEndTime,
			Date updateStartTime, Date updateEndTime) {
		Long total = clientInfoService.queryNumByClientInfo(clientInfo, createStartTime, createEndTime, updateStartTime,
				updateEndTime);
		List<ClientInfo> list = clientInfoService.queryByClientInfo(clientInfo, page, rows, createStartTime,
				createEndTime, updateStartTime, updateEndTime);
		Map map = new HashMap<>();
		map.put("total", total);
		map.put("rows", list);
		return map;
	}

	/**
	 * 添加客户
	 * @param cname
	 * @param contactName
	 * @param source
	 * @param phone
	 * @param tel
	 * @param remarks
	 * @return
	 */
	@PostMapping("/add-client")
	@ResponseBody
	public Map addClinet(String cname, String contactName, String source, String phone, String tel, String remarks, HttpServletRequest request) {
		try {
			Date date = new Date();
			HttpSession session = request.getSession();
			Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
			BigDecimal userId = new BigDecimal(userinfo.getUId());
			Map map = clientInfoService.addClientAndContract(cname, contactName, source, phone, tel, remarks,
					date, date, userId);
			return map;
		} catch (ServiceException e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 删除客户
	 * @param cid
	 * @return
	 */
	@PostMapping("/del-client")
	@ResponseBody
	public Result delClient(Long cid) {
		try {
			Result result = clientInfoService.delClient(cid);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(e);
		}
	}

	/**
	 * 编辑客户
	 * @param clientInfo
	 * @return
	 */
	@PostMapping("/edit-client")
	@ResponseBody
	public Result editClient(ClientInfo clientInfo) {
		Result result = clientInfoService.editClient(clientInfo);
		return result;
	}
	
	/**
	 * 根据用户编号获得用户名
	 * @param uId
	 * @return
	 */
	@PostMapping("/client-get-userName")
	@ResponseBody
	public String getUserName(Long uId) {
		return clientInfoService.userName(uId);
	}
	
	@PostMapping("/upload-excel-ClientInfo")
	@ResponseBody
	public Result uploadExcelClientInfo(@RequestParam(value="file",required = false)MultipartFile file, String uploadWay, HttpServletRequest request, HttpServletResponse response){
		return clientInfoService.uploadExcelClinetInfo(file, uploadWay);
	}
	
	@RequestMapping("/client-export-excel")
	public void exportExcelClientInfo(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException {
		List<ClientInfo> clientInfos = clientInfoService.findAll();
		ExportExcel ee= new ExportExcel();
		String[] headers = { "ID", "地址", "是否删除", "客户名称", "创建日期", "信用", "传真", "重要程度", "行业", "地区", "支付方式", "手机号码"
				, "公司简介", "关系等级", "备注", "规模", "客户来源", "阶段", "类型", "更新日期", "网址", "用户编号"};
		
		String fileName = "客户信息表";
		ee.exportExcel(headers,clientInfos,fileName,response);
	}
	
	
	@GetMapping("/client-group")
	@ResponseBody
	public List<ClientGroupCountVo> getClientGroupCountVo(String factor) {
		return clientInfoService.selClientGroupCountVo(factor);
	}
}
